// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Properties.java

package gnu.crypto;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.PropertyPermission;

public final class Properties
{

    private static void debug(String s)
    {
        err.println(">>> Properties: " + s);
    }

    private Properties()
    {
        reproducible = false;
        checkForWeakKeys = true;
        doRSABlinding = true;
        init();
    }

    public static final synchronized String getProperty(String key)
    {
        if(key == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission(key, "read"));
        key = key.trim().toLowerCase();
        if("".equals(key))
            return null;
        else
            return (String)props.get(key);
    }

    public static final synchronized void setProperty(String key, String value)
    {
        if(key == null || value == null)
            return;
        key = key.trim().toLowerCase();
        if("".equals(key))
            return;
        value = value.trim();
        if("".equals(value))
            return;
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission(key, "write"));
        if(key.equals("gnu.crypto.with.reproducible.prng") && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
            setReproducible(Boolean.valueOf(value).booleanValue());
        else
        if(key.equals("gnu.crypto.with.check.for.weak.keys") && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
            setCheckForWeakKeys(Boolean.valueOf(value).booleanValue());
        else
        if(key.equals("gnu.crypto.with.rsa.blinding") && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
            setDoRSABlinding(Boolean.valueOf(value).booleanValue());
        else
            props.put(key, value);
    }

    public static final synchronized boolean isReproducible()
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.reproducible.prng", "read"));
        return instance().reproducible;
    }

    public static final synchronized boolean checkForWeakKeys()
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.check.for.weak.keys", "read"));
        return instance().checkForWeakKeys;
    }

    public static final synchronized boolean doRSABlinding()
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.rsa.blinding", "read"));
        return instance().doRSABlinding;
    }

    public static final synchronized void setReproducible(boolean value)
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.reproducible.prng", "write"));
        instance().reproducible = value;
        props.put("gnu.crypto.with.reproducible.prng", String.valueOf(value));
    }

    public static final synchronized void setCheckForWeakKeys(boolean value)
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.check.for.weak.keys", "write"));
        instance().checkForWeakKeys = value;
        props.put("gnu.crypto.with.check.for.weak.keys", String.valueOf(value));
    }

    public static final synchronized void setDoRSABlinding(boolean value)
    {
        SecurityManager sm = System.getSecurityManager();
        if(sm != null)
            sm.checkPermission(new PropertyPermission("gnu.crypto.with.rsa.blinding", "write"));
        instance().doRSABlinding = value;
        props.put("gnu.crypto.with.rsa.blinding", String.valueOf(value));
    }

    private static final synchronized Properties instance()
    {
        if(singleton == null)
            singleton = new Properties();
        return singleton;
    }

    private void init()
    {
        props.put("gnu.crypto.with.reproducible.prng", (new Boolean(reproducible)).toString());
        props.put("gnu.crypto.with.check.for.weak.keys", (new Boolean(checkForWeakKeys)).toString());
        props.put("gnu.crypto.with.rsa.blinding", (new Boolean(doRSABlinding)).toString());
        String propFile = null;
        try
        {
            propFile = (String)AccessController.doPrivileged(new PrivilegedAction() {

                public Object run()
                {
                    return System.getProperty("gnu.crypto.properties.file");
                }

            }
);
        }
        catch(SecurityException se)
        {
            debug("Reading property gnu.crypto.properties.file not allowed. Ignored.");
        }
        if(propFile != null)
            try
            {
                java.util.Properties temp = new java.util.Properties();
                FileInputStream fin = new FileInputStream(propFile);
                temp.load(fin);
                temp.list(System.out);
                props.putAll(temp);
            }
            catch(IOException ioe)
            {
                debug("IO error reading " + propFile + ": " + ioe.getMessage());
            }
            catch(SecurityException se)
            {
                debug("Security error reading " + propFile + ": " + se.getMessage());
            }
        handleBooleanProperty("gnu.crypto.with.reproducible.prng");
        handleBooleanProperty("gnu.crypto.with.check.for.weak.keys");
        handleBooleanProperty("gnu.crypto.with.rsa.blinding");
        reproducible = (new Boolean((String)props.get("gnu.crypto.with.reproducible.prng"))).booleanValue();
        checkForWeakKeys = (new Boolean((String)props.get("gnu.crypto.with.check.for.weak.keys"))).booleanValue();
        doRSABlinding = (new Boolean((String)props.get("gnu.crypto.with.rsa.blinding"))).booleanValue();
    }

    private void handleBooleanProperty(String name)
    {
        String s = null;
        try
        {
            s = System.getProperty(name);
        }
        catch(SecurityException x)
        {
            debug("SecurityManager forbids reading system properties. Ignored");
        }
        if(s != null)
        {
            s = s.trim().toLowerCase();
            if(s.equals(TRUE) || s.equals(FALSE))
            {
                debug("Setting " + name + " to '" + s + "'");
                props.put(name, s);
            } else
            {
                debug("Invalid value for -D" + name + ": " + s + ". Ignored");
            }
        }
    }

    private static final String NAME = "Properties";
    private static final boolean DEBUG = true;
    private static final PrintWriter err;
    public static final String PROPERTIES_FILE = "gnu.crypto.properties.file";
    public static final String REPRODUCIBLE_PRNG = "gnu.crypto.with.reproducible.prng";
    public static final String CHECK_WEAK_KEYS = "gnu.crypto.with.check.for.weak.keys";
    public static final String DO_RSA_BLINDING = "gnu.crypto.with.rsa.blinding";
    private static final String TRUE;
    private static final String FALSE;
    private static final HashMap props = new HashMap();
    private static Properties singleton = null;
    private boolean reproducible;
    private boolean checkForWeakKeys;
    private boolean doRSABlinding;

    static 
    {
        err = new PrintWriter(System.out, true);
        TRUE = Boolean.TRUE.toString();
        FALSE = Boolean.FALSE.toString();
    }
}
